package com.avic.rong.search.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.search.bean.SearchHotBean;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by uncleChen on 2019/7/20.
 */
public class SearchViewHomeReMenRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private static final int Left = 0;
    private static final int Right = 1;
    private static int currentType = 0;

    private ArrayList<SearchHotBean> beans = new ArrayList<>();

    private SearchViewHomeReMenListener listener;

    public SearchViewHomeReMenListener getListener() {
        return listener;
    }

    public void setListener(SearchViewHomeReMenListener listener) {
        this.listener = listener;
    }

    public SearchViewHomeReMenRvAdapter(Context context) {
        this.context = context;
    }

    public void setBeans(ArrayList<SearchHotBean> beans) {
        this.beans = beans;
        notifyDataSetChanged();

    }



    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = null;
        if (getItemViewType(i) == Left) {
            view = LayoutInflater.from(context).inflate(R.layout.view_search_home_remen, viewGroup, false);
        }else {
            view = LayoutInflater.from(context).inflate(R.layout.view_search_home_remen_right, viewGroup, false);
        }
        return new SearchViewHomeReMenLeftRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {

            SearchViewHomeReMenLeftRvViewHolder holder = (SearchViewHomeReMenLeftRvViewHolder) viewHolder;
            SearchHotBean bean = beans.get(position);
            holder.tv_name.setText(bean.getWord());
            if (position == beans.size() - 1 || position == beans.size() - 2){
                holder.line_bottom.setVisibility(View.INVISIBLE);
            }else{
                holder.line_bottom.setVisibility(View.VISIBLE);
            }
            holder.itemView.setOnClickListener(new MyOnclickListener(bean));
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            currentType = Left;
        } else {
            currentType = Right;
        }
        return currentType;
    }


    @Override
    public int getItemCount() {
        if (beans.size() < 10)
            return beans.size();
        else
            return 10;
    }

    class SearchViewHomeReMenLeftRvViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.line_bottom)
        View line_bottom;

        public SearchViewHomeReMenLeftRvViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    class MyOnclickListener implements View.OnClickListener{

        SearchHotBean bean;

        public MyOnclickListener(SearchHotBean bean) {
            this.bean = bean;
        }

        @Override
        public void onClick(View v) {
            listener.remenGoResult(bean.getWord());
        }
    }

    public interface SearchViewHomeReMenListener {
        void remenGoResult(String searchStr);
    }
}
